import hashlib
import time
import unittest

import requests

gatewayId = "AABBCCDDEEFF"
MQTT_FACTORY_KEY = "B70BCBB05893A5BBE31A81226B85D64291F71E05"
wxUser = "AF5chKwUpWPDN9tECoBekFChOXcjOpJLwrV2H5zxFSv6AcmNs0udK7R9g7uzbnmi1TaAQlyfSrXdgQive7ItQYE_OBuFVTG55icwwAfyVIjPMx8gnCm_xyigSqi07fcGFSC0RbsWeSGZLfz-lQa0kOQh6KPTHiS1ofFDqr7QaHmCewlzzw=="


class MyTestCase(unittest.TestCase):
    def get(self, url, args=None):
        if args is None:
            args = {}
        baseUrl = "http://192.168.98.108:8899/gateway2"
        return requests.get(baseUrl + url, args).content.decode('utf8')

    def getWithSign(self, url, gatewayId, args={}):
        t = str(int(time.time()))
        signStr = MQTT_FACTORY_KEY + gatewayId + t

        m = hashlib.md5()
        m.update(signStr.encode('utf8'))
        sign = m.hexdigest()
        args['sign'] = sign
        args['time'] = t
        args['gatewayId'] = gatewayId
        return self.get(url, args)

    def test_获取时间(self):
        ret = self.get("/gateway/getTime")
        print(ret)

    def test_网关绑定用户(self):
        print(self.getWithSign("/gateway/resetAndRegister", gatewayId, {
            "wxUser": wxUser,
        }))

    def test_网关重置(self):
        print(self.getWithSign("/gateway/reset", gatewayId))

    def test_请求连接参数(self):
        print(self.getWithSign("/gateway/getConnectInfo", gatewayId))


if __name__ == '__main__':
    unittest.main()
